﻿using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Model.Map;
using KingsSharp.Core.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CRM.Admin.Areas.Customer.Controllers
{
    /// <summary>
    /// 营销地图数据功能
    /// </summary>
    public class GetMapDataController : Controller
    {
        // GET: Customer/GetMapData
        public IAreaRequestService AreaRequestService { get; set; }
        public ISupplyService SupplyService { get; set; }
        public IUserRequstService UserRequstService { get; set; }
        public ISaleService SaleService { get; set; }
        public INetworkService NetworkService { get; set; }
        public IIndustryRequstService IndustryRequstService { get; set; }
        public IAreaProfilesService AreaProfilesService { get; set; }
        /// <summary>
        /// 需求情况按地区
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public ActionResult GetAreaRequestGrid(int year, string areaType, string area)
        {


            if (areaType.Equals("Area"))
            {
                var data = AreaRequestService.AreaRequsts.Where(a => a.Area.Equals(area)).Where(a => a.Year == year);
                if (data.Count() > 0)
                {
                    var ReqwtSum = data.Sum(a => a.ReqWt);
                    return Json(ReqwtSum, JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("Province"))
            {
                var data = AreaRequestService.AreaRequsts.Where(a => a.Province.Equals(area)).Where(a => a.Year == year);
                if (data.Count() > 0)
                {
                    var ReqwtSum = data.Sum(a => a.ReqWt);
                    return Json(ReqwtSum, JsonRequestBehavior.AllowGet);
                }
            }
            else if (areaType.Equals("City"))
            {
                var data = AreaRequestService.AreaRequsts.Where(a => a.City.Equals(area)).Where(a => a.Year == year);
                if (data.Count() > 0)
                {
                    var ReqwtSum = data.Sum(a => a.ReqWt);
                    return Json(ReqwtSum, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(0, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 供给分析
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public ActionResult GetSupplyGrid(int year, string areaType, string area)
        {

            if (areaType.Equals("Area"))
            {
                var data = SupplyService.Supplys.Where(a => a.Year == year).Where(a => a.Area.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.SupWt), JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("Province"))
            {
                var data = SupplyService.Supplys.Where(a => a.Year == year).Where(a => a.Province.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.SupWt), JsonRequestBehavior.AllowGet);
                }

            }
            return Json(0, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 终端用户
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUserRequstGrid(int year, string areaType, string area)
        {


            if (areaType.Equals("Area"))
            {
                var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Area.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.ReqWt), JsonRequestBehavior.AllowGet);
                }
            }
            else if (areaType.Equals("Province"))
            {
                var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Province.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.ReqWt), JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("City"))
            {
                var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.City.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.ReqWt), JsonRequestBehavior.AllowGet);
                }
            }
            return Json(0, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 销售流向
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSaleGrid(int year, string areaType, string area)
        {

            if (areaType.Equals("Area"))
            {
                var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Area.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.SaleWt), JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("Province"))
            {
                var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Province.Equals(area));
                if (data.Count() > 0)
                {
                    return Json(data.Sum(a => a.SaleWt), JsonRequestBehavior.AllowGet);
                }
            }
            return Json(0, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 销售网点
        /// </summary>
        /// <returns></returns>
        public ActionResult GetNetworkService()
        {
            var data = NetworkService.Networks.Where(a => a.IsDeleted == false).Select(s => new
            {
                s.NetworkArea,
                s.Latitude,
                s.Longitude,
                s.NetworkName
            }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 简介查询
        /// </summary>
        /// <returns></returns>
        public ActionResult GetNetworkProfiles(string Couty)
        {
            var entity = NetworkService.Networks.FirstOrDefault(a => a.NetworkName.Equals(Couty));
            if (entity == null)
            {
                return null;
            }
            return Json(entity.Profiles, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 查询简介
        /// </summary>
        /// <param name="Profiles"></param>
        /// <returns></returns>
        public ActionResult GetAreaProfiles(string Area)
        {
            var entity = AreaProfilesService.AreaProfiles.FirstOrDefault(a => a.Area == Area).MapTo<AreaProfilesDto>();
            if (entity == null)
            {
                entity = new AreaProfilesDto();
            }
            return Json(entity, JsonRequestBehavior.AllowGet);
        }

        #region 品种分析
        /// <summary>
        /// 品种分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetProdGrid(int year, string areaType, string area, string industry)
        {

            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = AreaRequestService.AreaRequsts.Where(a => a.Year == year).
                             Where(a => a.Area.Equals(area)).GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.ReqWt))
                            .Select(s => new
                            {
                                s.Key,
                                Count = s.Sum(a => a.ReqWt)
                            }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year)
                        .Where(a => a.Area.Equals(area)).Where(a => a.Industry == industry)
                        .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.ReqWt))
                        .Select(s => new
                        {
                            s.Key,
                            Count = s.Sum(a => a.ReqWt)
                        }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = AreaRequestService.AreaRequsts.Where(a => a.Year == year).
                         Where(a => a.Province.Equals(area)).GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.ReqWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.ReqWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year)
                      .Where(a => a.Province.Equals(area)).Where(a => a.Industry == industry)
                      .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.ReqWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.ReqWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("City"))
            {

                var data = AreaRequestService.AreaRequsts.Where(a => a.Year == year).
                     Where(a => a.City.Equals(area)).GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.ReqWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.ReqWt)
                     }).ToList();
                return Json(data, JsonRequestBehavior.AllowGet);


            }
            return null;

        }
        /// <summary>
        /// 供给情况品种分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSupplyProd(int year, string area, string areaType,string industry)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = SupplyService.Supplys.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                    .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.SupWt))
                    .Select(s => new
                    {
                        s.Key,
                        Count = s.Sum(a => a.SupWt)
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = SupplyService.Supplys.Where(a => a.Year == year)
                        .Where(a => a.Area.Equals(area)).Where(a=>a.SteelFactory.Equals(industry))
                    .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.SupWt))
                    .Select(s => new
                    {
                        s.Key,
                        Count = s.Sum(a => a.SupWt)
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
            
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = SupplyService.Supplys.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.SupWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SupWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = SupplyService.Supplys.Where(a => a.Year == year)
                       .Where(a => a.Province.Equals(area)).Where(a => a.SteelFactory.Equals(industry))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(s => s.SupWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SupWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            return null;
        }
        /// <summary>
        /// 供给情况钢厂分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSupplySteelFactory(int year, string area, string areaType,string prod,int type)
        {
           
            if (areaType.Equals("Area"))
            {
                var list = SupplyService.Supplys;
                if (type == 1)
                {
                   list=list.Where(a => a.SteelFactoryArea.Equals("四川"));
                }
                else if (type==2)
                {
                    list=list.Where(a => a.SteelFactoryArea != "四川");
                }
                if (string.IsNullOrEmpty(prod))
                {
                    var data = list.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                   .GroupBy(a => a.SteelFactory).OrderByDescending(a => a.Sum(c => c.SupWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SupWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = list.Where(a => a.Year == year)
                        .Where(a => a.Area.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                        .GroupBy(a => a.SteelFactory).OrderByDescending(a => a.Sum(c => c.SupWt))
                        .Select(s => new
                        {
                            s.Key,
                            Count = s.Sum(a => a.SupWt)
                        }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                var list = SupplyService.Supplys;
                if (type == 1)
                {
                   list= list.Where(a => a.SteelFactoryArea.Equals(area));
                }
                else if (type == 2)
                {
                   list=list.Where(a => a.SteelFactoryArea != area);
                }
                if (string.IsNullOrEmpty(prod))
                {
                    var data = list.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                   .GroupBy(a => a.SteelFactory).OrderByDescending(a => a.Sum(c => c.SupWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SupWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = list.Where(a => a.Year == year).
                        Where(a => a.Province.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                     .GroupBy(a => a.SteelFactory).OrderByDescending(a => a.Sum(c => c.SupWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.SupWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                
            }
            return null;
        }
        /// <summary>
        /// 终端用户品种分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUserRequstProd(int year, string area, string areaType,string industry)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year)
                        .Where(a => a.Area.Equals(area)).Where(a=>a.Industry.Equals(industry))
                      .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.ReqWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year)
                       .Where(a => a.Province.Equals(area)).Where(a => a.Industry.Equals(industry))
                     .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.ReqWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("City"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.City.Equals(area))
                     .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.ReqWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year)
                      .Where(a => a.City.Equals(area)).Where(a => a.Industry.Equals(industry))
                       .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.ReqWt))
                    .Select(s => new
                    {
                        s.Key,
                        Count = s.Sum(a => a.ReqWt)
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
          
            }
            return null;
        }
        /// <summary>
        /// 销售流向品种分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSaleProd(int year, string area, string areaType,string industry)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.SaleWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SaleWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                  var data = SaleService.Sales.Where(a => a.Year == year)
                        .Where(a => a.Area.Equals(area)).Where(a=>a.Industry.Equals(industry))
                         .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.SaleWt))
                         .Select(s => new
                         {
                             s.Key,
                             Count = s.Sum(a => a.SaleWt)
                         }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(industry))
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                   .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.SaleWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SaleWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = SaleService.Sales.Where(a => a.Year == year)
                    .Where(a => a.Province.Equals(area)).Where(a => a.Industry.Equals(industry))
                     .GroupBy(a => a.ProdName).OrderByDescending(a => a.Sum(c => c.SaleWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.SaleWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            return null;
        }
        #endregion
        #region 行业分析
        /// <summary>
        /// 行业分析
        /// </summary>
        /// <returns></returns>
        public ActionResult IndustryRequst(int year, string areaType, string area,string prod)
        {

            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year).
                   Where(a => a.Area.Equals(area)).GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year).
                   Where(a => a.Area.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                   .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year).
                   Where(a => a.Province.Equals(area)).GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = IndustryRequstService.IndustryRequsts.Where(a => a.Year == year).
                      Where(a => a.Province.Equals(area)).Where(a => a.ProdName.Equals(prod))
                      .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.ReqWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            return null;

        }
        /// <summary>
        /// 终端用户行业分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUserRequsrIndustry(int year, string area, string areaType,string prod)
        {
            if (areaType.Equals("Area"))
            {

                if (string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                   .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.ReqWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).
                        Where(a => a.Area.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                       .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                       .Select(s => new
                       {
                           s.Key,
                           Count = s.Sum(a => a.ReqWt)
                       }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                      .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.ReqWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year)
                        .Where(a => a.Province.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                     .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.ReqWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("City"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year).Where(a => a.City.Equals(area))
                          .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                          .Select(s => new
                          {
                              s.Key,
                              Count = s.Sum(a => a.ReqWt)
                          }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = UserRequstService.UserRequsts.Where(a => a.Year == year)
                       .Where(a => a.City.Equals(area)).Where(a => a.ProdName.Equals(prod))
                    .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.ReqWt))
                    .Select(s => new
                    {
                        s.Key,
                        Count = s.Sum(a => a.ReqWt)
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
       
            }
            return null;
        }
        /// <summary>
        /// 查询需求量前十的终端用户
        /// </summary>
        /// <param name="year"></param>
        /// <param name="area"></param>
        /// <param name="areaType"></param>
        /// <returns></returns>
        public ActionResult UserRequst(int year, string area, string areaType,string prod,string industry)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(prod) && string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Area.Equals(area)).Take(10)
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                        .Where(a => a.Year == year).Where(a => a.ProdName.Equals(prod))
                     .Where(a => a.Area.Equals(area)).Take(10)
                     .Select(s => new
                     {
                         s.FinUserName,
                         s.ReqWt
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                       .Where(a => a.Year == year).Where(a => a.Industry.Equals(industry))
                    .Where(a => a.Area.Equals(area)).Take(10)
                    .Select(s => new
                    {
                        s.FinUserName,
                        s.ReqWt
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(prod)&&string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                  .Where(a => a.Province.Equals(area)).Take(10)
                  .Select(s => new
                  {
                      s.FinUserName,
                      s.ReqWt
                  }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                        .Where(a => a.Year == year).Where(a => a.ProdName.Equals(prod))
                     .Where(a => a.Province.Equals(area)).Take(10)
                     .Select(s => new
                     {
                         s.FinUserName,
                         s.ReqWt
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                       .Where(a => a.Year == year).Where(a => a.Industry.Equals(industry))
                    .Where(a => a.Province.Equals(area)).Take(10)
                    .Select(s => new
                    {
                        s.FinUserName,
                        s.ReqWt
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("City"))
            {
                if (string.IsNullOrEmpty(prod)&&string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                  .Where(a => a.City.Equals(area)).Take(10)
                  .Select(s => new
                  {
                      s.FinUserName,
                      s.ReqWt
                  }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                        .Where(a => a.Year == year).Where(a => a.ProdName.Equals(prod))
                     .Where(a => a.City.Equals(area)).Take(10)
                     .Select(s => new
                     {
                         s.FinUserName,
                         s.ReqWt
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt)
                       .Where(a => a.Year == year).Where(a => a.Industry.Equals(industry))
                    .Where(a => a.City.Equals(area)).Take(10)
                    .Select(s => new
                    {
                        s.FinUserName,
                        s.ReqWt
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            return null;

        }
        /// <summary>
        /// 根据地区查询所有的终端用户
        /// </summary>
        /// <param name="year"></param>
        /// <param name="area"></param>
        /// <param name="areaType"></param>
        /// <returns></returns>
        public ActionResult UserRequstAll(int year, string area, string areaType,string prod,string industry)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(prod) && string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Area.Equals(area))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Area.Equals(area)).Where(a => a.ProdName.Equals(prod))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Area.Equals(area)).Where (a=>a.Industry.Equals(industry))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(prod)&&string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                  .Where(a => a.Province.Equals(area))
                  .Select(s => new
                  {
                      s.FinUserName,
                      s.ReqWt
                  }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Province.Equals(area)).Where(a => a.ProdName.Equals(prod))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.Province.Equals(area)).Where(a => a.Industry.Equals(industry))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            else if (areaType.Equals("City"))
            {
                if (string.IsNullOrEmpty(prod)&&string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                    .Where(a => a.City.Equals(area))
                    .Select(s => new
                    {
                        s.FinUserName,
                        s.ReqWt
                    }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(prod))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.City.Equals(area)).Where(a => a.ProdName.Equals(prod))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (!string.IsNullOrEmpty(industry))
                {
                    var data = UserRequstService.UserRequsts.OrderByDescending(a => a.ReqWt).Where(a => a.Year == year)
                   .Where(a => a.City.Equals(area)).Where(a => a.Industry.Equals(industry))
                   .Select(s => new
                   {
                       s.FinUserName,
                       s.ReqWt
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }


            }
            return null;

        }
        /// <summary>
        /// 销售流向行业分析
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSaleIndustry(int year, string area, string areaType,string prod)
        {
            if (areaType.Equals("Area"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Area.Equals(area))
                   .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.SaleWt))
                   .Select(s => new
                   {
                       s.Key,
                       Count = s.Sum(a => a.SaleWt)
                   }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).
                        Where(a => a.Area.Equals(area)).Where(a=>a.ProdName.Equals(prod))
                      .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.SaleWt))
                      .Select(s => new
                      {
                          s.Key,
                          Count = s.Sum(a => a.SaleWt)
                      }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
               
            }
            else if (areaType.Equals("Province"))
            {
                if (string.IsNullOrEmpty(prod))
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).Where(a => a.Province.Equals(area))
                          .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.SaleWt))
                          .Select(s => new
                          {
                              s.Key,
                              Count = s.Sum(a => a.SaleWt)
                          }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = SaleService.Sales.Where(a => a.Year == year).
                       Where(a => a.ProdName.Equals(area)).Where(a => a.ProdName.Equals(prod))
                     .GroupBy(a => a.Industry).OrderByDescending(a => a.Sum(c => c.SaleWt))
                     .Select(s => new
                     {
                         s.Key,
                         Count = s.Sum(a => a.SaleWt)
                     }).ToList();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
             
            }
            return null;
        }
        #endregion
    }
}